//
//  VideoDetailVC.m
//  LiteraryTune
//
//  Created by 陈雪丹 on 2018/5/31.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "VideoDetailVC.h"
#import "VideoListModel.h"
#import "ChapterTableViewCell.h"
#import "MediaPlayerVC.h"

@interface VideoDetailVC () <UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>

@property (nonatomic, strong)UITableView *tableView;
//数据源
@property (nonatomic, strong)VideoListModel *dataModel;
@property (nonatomic, strong) NSString * imgStr;

@end

@implementation VideoDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getNetData];
    [self.view addSubview:self.tableView];
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
}

- (void)getNetData {
    [CXDNetHelp getRequestWithURLString:[NSString stringWithFormat:@"%@%@.shtml",VideoDetailURL,_videoId] parameters:nil success:^(id  _Nullable responseObject) {
        _imgStr = [responseObject objectForKey:@"spic"];
        _dataModel = [VideoListModel yy_modelWithJSON:responseObject];
        [self.tableView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - CXDTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 50;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ChapterTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ChapterTableViewCell class])];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataModel.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChapterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ChapterTableViewCell class])];
    VideoDetailLisyModel *model = _dataModel.list[indexPath.row];
    cell.nameLabel.text = [NSString stringWithFormat:@"%ld %@",indexPath.row + 1,model.title];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (![CXDHelper getUserLoginStatus] && [NSUserDefaults.standardUserDefaults boolForKey:K_NeedLogin]) {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }else {
        VideoDetailLisyModel *model = _dataModel.list[indexPath.row];
        MediaPlayerVC *detailVC = [[MediaPlayerVC alloc] init];
        detailVC.navigationItem.title = model.title;
        detailVC.imgString = [NSString stringWithFormat:@"%@%@",RootHomeURL,self.imgStr];
        detailVC.urlString = [NSString stringWithFormat:@"%@%@",RootHomeURL,model.url];
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

@end
